<?php
include("config.php");

require_once(BIB_ADODB);

require_once(FWK_CONTROL."ControlSmarty.class.php");
require_once(FWK_CONTROL."ControlSessaoSite.class.php");
require_once(FWK_CONTROL."ControlDB.class.php");

require_once(BIB_PEARL);
require_once(BIB_SMARTY);

require_once(FWK_UTIL."FormataCampos.class.php");
require_once(FWK_UTIL."Utf8Parsers.class.php");
require_once(FWK_UTIL."FormataDatas.class.php");
require_once(FWK_UTIL."FormataString.class.php");
require_once(FWK_UTIL."HttpUtil.class.php");

require_once(FWK_EXCEPTION."CustonException.class.php");
require_once(BIB_FWK."adodb/adodb-errorpear.inc.php");

require_once(PASTA_FWK."tags.php");
require_once(PASTA_DAO."HistoricoVenda.class.php");
require_once(PASTA_DAO."Transacao.class.php");

//biblioteca de controle dos templates
$objCtrlSmarty = ControlSmarty::getSmarty();

//título da pagina
$objCtrlSmarty->assign("TITULO_PAG",TITULO_SITE);
$objCtrlSmarty->assign("LINK_SITE",LINK_SITE);
//definição dos estilos do site, se necessário acrescentar outro, apenas concatenar à string com \n no final.
$strCss = "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/estiloSite.css\" />\n";
$strCss .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/jquery.lightbox-0.5.css\" />\n";
$strCss .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/coin-slider-styles.css\" />\n";
$strCss .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/facebox.css\" />\n";
$strCss .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/bx_styles.css\" />\n";
$strCss .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/estilo.css\" />\n";

if(strpos($_SERVER["HTTP_USER_AGENT"], "MSIE") > 0)
	$strCss .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/estilosIE.css\" />\n";

$objCtrlSmarty->assign("CSS_SITE",$strCss);

$strJs = "<script type=\"text/javascript\" src=\"js/jquery.js\"></script>\n";
//$strJs = "<script type=\"text/javascript\" src=\"js/jquery.maskedinput-1.3.min.js\"></script>\n";
$strJs .= "<script type=\"text/javascript\" src=\"js/jquery.lightbox-0.5.js\"></script>\n";
$strJs .= "<script type=\"text/javascript\" src=\"js/coin-slider.min.js\"></script>\n";
$strJs .= "<script type=\"text/javascript\" src=\"js/jquery.bxSlider.min.js\"></script>\n";
$strJs .= "<script type=\"text/javascript\" src=\"js/facebox.js\"></script>\n";
$strJs .= "<script type=\"text/javascript\" src=\"js/reflection.js\"></script>\n";
$strJs .= "<script type=\"text/javascript\" src=\"js/actions.js\"></script>\n";
$strJs .= "<script type=\"text/javascript\" src=\"js/acoesSite.js\"></script>\n";
$strJs .= "<script type=\"text/javascript\" src=\"js/jqFancyTransitions.js\"></script>\n";
$objCtrlSmarty->assign("JS_SITE",$strJs);

$objCtrlSmarty->assign("PAG_ATUAL",base64_encode($_SERVER["REQUEST_URI"]));
$objCtrlSmarty->assign("PAG_ATUAL_FULL","http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);

//SESSAO
$objSessao = new ControlSessaoSite();
$objUsrSession = $objSessao->getObjSessao(SESSAO_SITE);
$arrDadosSessao = $objSessao->getArrSessao(SESSAO_SITE);

//Qtd pontos ranking
$qtdMinimaPontosRanking = 2000;

if($objUsrSession->getCpfUsuario() != null) {
		
	$objHistoricoVenda = new HistoricoVenda();
	$objTransacao = new Transacao();
	
	$valorTotalPontos = $objHistoricoVenda->getValorTotalPontosPorUsuario($objUsrSession->getCpfUsuario());
	$valorTotalTransacoes = $objTransacao->getValorTotalTransacoesPorUsuario($objUsrSession->getIdUsuario());
	
	$objCtrlSmarty->assign("saldo_total_usuario",($valorTotalPontos[0] - $valorTotalTransacoes[0]));
	
	
	//Pontos para expirar
	$arrPontosExpirar = Utf8Parsers::matrizUtf8Encode($objHistoricoVenda->getHistoricoVendasAExpirar($objUsrSession->getCpfUsuario()));
	$totalPontosAteDataAtual = Utf8Parsers::arrayUtf8Encode($objHistoricoVenda->getTotalPontosAteDataAtual($objUsrSession->getCpfUsuario()));
	
	$arrPontosExpirados = array();
	//Verifica se e para expirar os pontos
	if(count($arrPontosExpirar) > 0) {
		if(($totalPontosAteDataAtual[0] - $valorTotalTransacoes[0]) > 0) {				
			$arrPontosExpirados["id_usuario"] = $objUsrSession->getIdUsuario();
			$arrPontosExpirados["tipo_transacao"] = 2;
			$arrPontosExpirados["valor_transacao"] = ($totalPontosAteDataAtual[0] - $valorTotalTransacoes[0]);
			$objTransacao->alterar(null,$arrPontosExpirados,null);
			
			foreach ($arrPontosExpirar as $historico_venda) {
				$objHistoricoVenda = new HistoricoVenda();
				$arrHistoricoAlteracao = $objHistoricoVenda->buscaCampos($historico_venda["id_vendas"]);
			 	$arrHistoricoAlteracao["expirado"] = 1;
			 	if($arrHistoricoAlteracao["id_usuario"] == null)
			 		$arrHistoricoAlteracao["id_usuario"] = 0;
			 	$objHistoricoVenda->alterar($arrHistoricoAlteracao["id_vendas"], $arrHistoricoAlteracao, null);
			 	$objHistoricoVenda = null;
			} 
		}
	}
}


//$totalPontosUsuario = Utf8Parsers::arrayUtf8Encode($objHistoricoVenda->getTotalPontosHistoricoVendas($objUsrSession->getCpfUsuario()));

//$totalPontosVencidosCancelados = Utf8Parsers::arrayUtf8Encode($objHistoricoVenda->getTotalPontosVencidosCancelados($objUsrSession->getCpfUsuario()));

//$totalPontosResgatados = Utf8Parsers::arrayUtf8Encode($objVoucher->getTotalPontosResgatados($objUsrSession->getIdUsuario()));

//$valorSaldoUsuario = Utf8Parsers::matrizUtf8Encode($objHistoricoVenda->getValorSaldoUsuario($objUsrSession->getCpfUsuario(), $objUsrSession->getIdUsuario()));
//$objCtrlSmarty->assign("saldo_total_usuario",($totalPontosUsuario[0] - ($totalPontosVencidosCancelados[0] + $totalPontosResgatados[0])));

//Variáveis

$objCtrlSmarty->assign("NOME_USUARIO",$objUsrSession->getNomeUsuario());


//define o cabeçaho do site
$objCtrlSmarty->assign("CABECALHO",$objCtrlSmarty->fetch("cabecalho.tpl"));


//define o rodape do site
$objCtrlSmarty->assign("RODAPE",$objCtrlSmarty->fetch("rodape.tpl"));

if(isset($_GET["msg"])){
	switch($_GET["msg"]){
		case "1":
			HttpUtil::exibeMsg("Sessão expirada, por favor logue novamente!");
			break;
		case "2":
			HttpUtil::exibeMsg("Erro ao enviar o Email, tente novamente mais tarde!");
			break;
		case "3":
			HttpUtil::exibeMsg("Senha Alterada com sucesso!");
			break;
		case "4":
			HttpUtil::exibeMsg("Mensagem enviada com sucesso!");
			break;
		case "5":
			HttpUtil::exibeMsg("Opinião enviada com sucesso!");
			break;
		default:
			break;
	}
}



function debug($arrDebug){
	print("<pre>");
	print_r($arrDebug);
	die();
}


?>